package com.view;
import android.app.ActionBar;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.bean.AreaBean;
import com.bean.CityBean;
import com.bean.ProvinceBean;
import com.bean.User;
import com.bigkoo.pickerview.OptionsPickerView;
import com.firewarning.R;
import com.service.UserService;
import com.util.DBManager;
import java.util.ArrayList;
public class RegisterActivity extends BaseActivity{
    private EventHandler handler=new EventHandler();
   private EditText editName;
   private EditText editPhone;
    private TextView editProvince;
    private TextView editCity;
    private TextView editArea;
    private EditText editDetail;
    private EditText editPassword;
    private EditText editPassword2;
    private Button   submit;
    private Button   reset;
    private OptionsPickerView pvOptions;//地址选择器
    private ArrayList<ProvinceBean> options1Items = new ArrayList<>();//省
    private ArrayList<ArrayList<CityBean>> options2Items = new ArrayList<>();//市
    private ArrayList<ArrayList<ArrayList<AreaBean>>> options3Items = new ArrayList<>();//区
    private ArrayList<String> Provincestr = new ArrayList<>();//省
    private ArrayList<ArrayList<String>> Citystr = new ArrayList<>();//市
    private ArrayList<ArrayList<ArrayList<String>>> Areastr = new ArrayList<>();//区
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        if(actionBar!=null) {
            actionBar.hide();
        }
        initView();
        initData();
    }
    private void initView() {

        editName=(EditText)findViewById(R.id.register_name);
        editName.setOnClickListener(handler);
        editPhone=(EditText)findViewById(R.id.register_phone);
        editPhone.setOnClickListener(handler);
        editProvince=(TextView) findViewById(R.id.register_province);
        editProvince.setOnClickListener(handler);
        editCity=(TextView) findViewById(R.id.register_city);
        editCity.setOnClickListener(handler);
        editCity.setInputType(InputType.TYPE_NULL);
        editArea=(TextView) findViewById(R.id.register_area);
        editArea.setOnClickListener(handler);
        editArea.setInputType(InputType.TYPE_NULL);
        editDetail=(EditText)findViewById(R.id.register_detail);
        editDetail.setOnClickListener(handler);
        editPassword=(EditText)findViewById(R.id.register_password);
        editPassword.setOnClickListener(handler);
        editPassword2=(EditText) findViewById(R.id.register_password2);
        editPassword2.setOnClickListener(handler);
        submit=(Button)findViewById(R.id.register_submit);
        submit.setOnClickListener(handler);
        reset=(Button)findViewById(R.id.register_reset);
        reset.setOnClickListener(handler);
    }

    private void initData() {
        //选项选择器
        pvOptions = new OptionsPickerView(this);
        // 获取数据库
        SQLiteDatabase db = DBManager.getdb(getApplication());
        //省
        Cursor cursor = db.query("province", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            int pro_id = cursor.getInt(0);
            String pro_code = cursor.getString(1);
            String pro_name = cursor.getString(2);
            String pro_name2 = cursor.getString(3);
            ProvinceBean provinceBean = new ProvinceBean(pro_id, pro_code, pro_name, pro_name2);
            options1Items.add(provinceBean);//添加一级目录
            Provincestr.add(cursor.getString(2));
            //查询二级目录，市区
            Cursor cursor1 = db.query("city", null, "province_id=?", new String[]{pro_id + ""}, null, null,
                    null);
            ArrayList<CityBean> cityBeanList = new ArrayList<>();
            ArrayList<String> cityStr = new ArrayList<>();
            //地区集合的集合（注意这里要的是当前省份下面，当前所有城市的地区集合我去）
            ArrayList<ArrayList<AreaBean>> options3Items_03 = new ArrayList<>();
            ArrayList<ArrayList<String>> options3Items_str = new ArrayList<>();
            while (cursor1.moveToNext()) {
                int cityid = cursor1.getInt(0);
                int province_id = cursor1.getInt(1);
                String code = cursor1.getString(2);
                String name = cursor1.getString(3);
                String provincecode = cursor1.getString(4);
                CityBean cityBean = new CityBean(cityid, province_id, code, name, provincecode);
                //添加二级目录
                cityBeanList.add(cityBean);
                cityStr.add(cursor1.getString(3));
                //查询三级目录
                Cursor cursor2 = db.query("area", null, "city_id=?", new String[]{cityid + ""}, null, null,
                        null);
                ArrayList<AreaBean> areaBeanList = new ArrayList<>();
                ArrayList<String> areaBeanstr = new ArrayList<>();
                while (cursor2.moveToNext()) {
                    int areaid = cursor2.getInt(0);
                    int city_id = cursor2.getInt(1);
//                    String code0=cursor2.getString(2);
                    String areaname = cursor2.getString(3);
                    String citycode = cursor2.getString(4);
                    AreaBean areaBean = new AreaBean(areaid, city_id, areaname, citycode);
                    areaBeanList.add(areaBean);
                    areaBeanstr.add(cursor2.getString(3));
                }
                options3Items_str.add(areaBeanstr);//本次查询的存储内容
                options3Items_03.add(areaBeanList);
            }
            options2Items.add(cityBeanList);//增加二级目录数据
            Citystr.add(cityStr);
            options3Items.add(options3Items_03);//添加三级目录
            Areastr.add(options3Items_str);
        }
        //设置三级联动效果
        pvOptions.setPicker(Provincestr, Citystr, Areastr, true);
        //设置选择的三级单位
//        pvOptions.setLabels("省", "市", "区");
        pvOptions.setTitle("选择城市");
        //设置是否循环滚动
        pvOptions.setCyclic(false, false, false);
        //设置默认选中的三级项目
        //监听确定选择按钮
        pvOptions.setSelectOptions(0, 0, 0);
        pvOptions.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3) {
                //返回的分别是三个级别的选中位置
                editProvince.setText(options1Items.get(options1).getPro_name());
                editCity.setText(options2Items.get(options1).get(option2).getName());
                editArea.setText(options3Items.get(options1).get(option2).get(options3).getName());

            }
        });
    }
    class EventHandler implements View.OnClickListener{
        @Override
        public void onClick(View view) {
            switch(view.getId()){
                case R.id.register_province:
                case R.id.register_city:
                case R.id.register_area:
                    pvOptions.show();
                    break;
                case R.id.register_reset:
                    editName.setText("");
                    editPhone.setText("");
                    editProvince.setText("");
                     editCity.setText("");
                    editArea.setText("");
                    editDetail.setText("");
                    editPassword.setText("");
                    editPassword2.setText("");break;
                case R.id.register_submit:
                    String name=editName.getText().toString().trim();
                    String phone=editPhone.getText().toString().trim();
                    String province=editProvince.getText().toString().trim();
                    String city=editCity.getText().toString().trim();
                    String area=editArea.getText().toString().trim();
                    String detail=editDetail.getText().toString().trim();
                    String password1= editPassword.getText().toString().trim();
                    String password2= editPassword2.getText().toString().trim();
                   if("".equals(name)||"".equals(phone)||"".equals(province)||"".equals(city)||"".equals(area)||"".equals(detail)||"".equals(password1)||"".equals(password2)){
                       Toast.makeText(RegisterActivity.this,"输入不能为空",Toast.LENGTH_SHORT).show();
                   }
                    else if(!password1.equals(password2)){
                        Toast.makeText(RegisterActivity.this,"密码不一致",Toast.LENGTH_SHORT).show();
                    }else if(UserService.checkUser(RegisterActivity.this,phone,password1)!=null){
                       Toast.makeText(RegisterActivity.this,"该用户已存在",Toast.LENGTH_SHORT).show();
                   }
                    else{
                       User user=new User(phone,name,province+city+area+detail,password1);
                       if( UserService.addUser(RegisterActivity.this,user))
                       RegisterActivity.this.startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
                    }

            }
        }
    }
}
